<?php
namespace Common\Model;
use Think\Model;
class ChatCustModel extends Model {
	protected $connection = 'DB_BASE_DSN';
	protected $tableName = 'qyx_chat_cust'; 
	
	
	public function getCustInfoByChatId($chatid,$status="1"){
		$map['chatid']  = $chatid;
		if($status === "0" || $status === "1")$map['status']  = $status;
		
		$fields = array('chatid','custid','nickname','msgsetting','status','createtime','updatetime');
		$result = $this->field($fields)->where($map)->select();
		return $result;
	}
	
	public function getCustIdsByChatId($chatid){
		$map['chatid']  = $chatid;
		$map['status']  = 1;
		
		$fields = array('custid');
		$result = $this->field($fields)->where($map)->select();
		return $result;
	}
	
	
	public function getChatIdsByCustId($custid){
		$map['custid']  = $custid;
		$map['status']  = 1;
	
		$fields = array('chatid','msgsetting');
		$result = $this->field($fields)->where($map)->select();
		return $result;
	}
	
	public function addCust($data){
		$map['chatid'] = $data['chatid'];
		$map['custid'] = $data['custid'];
		$ret = $this->where($map)->find();
		if(!empty($ret)){
			return $ret['id'];
		}else{
			$result = $this->add($data);
			return $result;
		}
	}
	
	public function getCust($chatid,$custid){
		$map['chatid'] = $chatid;
		$map['custid'] = $custid;
		$result = $this->where($map)->find();
		return $result;
	}
	
	public function updateCust($data){
		$map['chatid'] = $data['chatid'];
		$map['custid'] = $data['custid'];
		$data['updatetime'] = date("Y-m-d H:i:s");
		$result = $this->where($map)->save($data);
		return $result;
	}
	
	public function deleteCust($data){
		$map['chatid'] = $data['chatid'];
		if(!empty($data['custids']) && is_array($data['custids']))$map['custid'] = array('in' , $data['custids']);
		if(!empty($data['custid']))$map['custid'] = $data['custid'];
		
		$result = $this->where($map)->delete();
		return $result;
	}
}


